//
//  ViewController.m
//  test
//
//  Created by student on 11/12/13.
//  Copyright (c) 2013 student. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)get:(id)sender {
    NSString* urlText = self.urlText.text;
    NSURL* url=[NSURL URLWithString:urlText];
    NSURLSession* session=[NSURLSession sharedSession];
    NSURLSessionDownloadTask* task=[session downloadTaskWithURL:url completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error){
        if(error){
            return;
        }
        NSInteger status = [(NSHTTPURLResponse*) response statusCode];
        if(status!=200){
                return;
        }
        NSData* d=[NSData dataWithContentsOfURL:location];
        UIImage *im=[UIImage imageWithData:d];
        
        dispatch_async(dispatch_get_main_queue(), ^{
        
            self.imageView.image = im;
        });
    }
                            ];
        [task resume];
}
- (IBAction)post:(id)sender {
    
    NSString* urlText = self.urlText.text;
    
    NSString* file_name = self.fileNameTextBox.text;
    
    NSString* postData=[NSString stringWithFormat:@"file=%@",file_name];
    
    
    NSURL* url=[NSURL URLWithString:urlText];
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPBody=[postData dataUsingEncoding:NSUTF8StringEncoding];
    request.HTTPMethod=@"POST";
    
    NSURLSession* session=[NSURLSession sharedSession];
    NSURLSessionDownloadTask* task=[session downloadTaskWithRequest:request completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error){
        if(error){
            return;
        }
        NSInteger status = [(NSHTTPURLResponse*) response statusCode];
        if(status!=200){
            return;
        }
        NSData* d=[NSData dataWithContentsOfURL:location];
        UIImage *im=[UIImage imageWithData:d];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            self.imageView.image = im;
        });
    }
                                    ];
    [task resume];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)buttonhello:(id)sender {
    self.TextBox.text = @"Hello";
}
- (IBAction) unwind:(UIStoryboardSegue*) segue{}

- (BOOL)textFieldShouldReturn: (UITextField*) tf {
    [tf resignFirstResponder];
    return YES;
}
- (IBAction)Longpress:(id)sender {
    self.TextBox.text = @"Hello";
}
- (IBAction)saveButtonTouch:(id)sender {
    NSFileManager* fm = [NSFileManager new];
    NSError* err = nil;
    NSURL* docURL= [fm URLForDirectory:NSApplicationSupportDirectory
               inDomain:NSUserDomainMask
      appropriateForURL:nil
                 create:YES
                  error:&err];
    
    NSString* fileName = self.fileNameTextBox.text;
    NSString* data = self.wordsTextBox.text;
    
    [data writeToURL:[docURL URLByAppendingPathComponent:fileName]
           atomically:YES
             encoding:NSUTF8StringEncoding
               error:&err];
}
- (IBAction)loadButtonTouch:(id)sender {
    NSFileManager* fm = [NSFileManager new];
    NSError* err = nil;
    NSURL* docURL = [fm URLForDirectory:NSApplicationSupportDirectory
               inDomain:NSUserDomainMask
      appropriateForURL:nil
                 create:YES
                  error:&err];
    
    NSString* fileName = self.fileNameTextBox.text;
    NSString* data = [NSString stringWithContentsOfURL:[ docURL URLByAppendingPathComponent:fileName]
                                               encoding:NSUTF8StringEncoding
                                                  error:&err];
                      self.wordsTextBox.text=data;
}
                      @end
